package cn.ren.controller;

import cn.ren.constant.MessageConstant;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.entity.Result;
import cn.ren.pojo.Member;
import cn.ren.pojo.OrderSetting;
import cn.ren.service.OrderSettingService;
import cn.ren.util.POIUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static cn.ren.util.POIUtils.readExcel;

/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-02-25 23:04
 **/
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        //通过工具类读取上传的excel文件
        try {
//            List<String[]> list = POIUtils.readExcel(excelFile);
            List<String[]> list = readExcel(excelFile);
            if (list != null && list.size() > 0){
                //遍历集合数据并保存到对象中
                List<OrderSetting> orderList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                    orderList.add(orderSetting);
                }
                //通过service来保存orderList到数据库
                orderSettingService.add(orderList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * 获得预约列表详情
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findordersettingList")
    public PageResult findordersettingList(@RequestBody QueryPageBean queryPageBean){
        return orderSettingService.findByCondition(queryPageBean);
    }

    /**
     * 删除预约
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public Result del(int id){
        try {
            orderSettingService.del(id);
            return new Result(true,"删除预约成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除预约失败");
        }
    }

    /**
     * 后台新增预约订单
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Member member,Integer[] setmealIds){
        try {
            Result result = orderSettingService.addNewOrderByBackEnd(member,setmealIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
